package com.sakura.test1;
 
import org.openqa.selenium.*;
import org.openqa.selenium.ie.InternetExplorerDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class DataReptile {

    private static InternetExplorerDriverService internetExplorerDriverService;

    public static WebDriver getIeDriver() throws IOException {
        System.setProperty("webdriver.ie.driver","C:\\Users\\Sakura\\Desktop\\gitspace\\test1\\IEDriverServer.exe");
        internetExplorerDriverService = new InternetExplorerDriverService.Builder().usingDriverExecutable(new File("C:\\Users\\Sakura\\Desktop\\gitspace\\test1\\IEDriverServer.exe")).usingAnyFreePort().build();
        internetExplorerDriverService.start();
        RemoteWebDriver remoteWebDriver = new RemoteWebDriver(internetExplorerDriverService.getUrl(), DesiredCapabilities.internetExplorer());
        return remoteWebDriver;
    }
 
    public static void dataReptile(String url) throws IOException {

        WebDriver driver = DataReptile.getIeDriver();
        Dimension dimension = new Dimension(1000, 1300);
        driver.manage().window().setSize(dimension);
        if (url.contains("taobao")) {
            driver.get("https://login.taobao.com/member/login.jhtml?redirectURL=https%3A%2F%2Fai.taobao.com%2F%3Fpid%3Dmm_121122127_40670994_68992200449");
            driver.findElement(By.xpath("//*[@id=\"J_SubmitQuick\"]")).click();

            String title = "";
            String price = "";
            String number = "";
            String titleImage = "";
            try {
                driver.get(url);
                for (int i = 0; i < 10; i++) {
                    ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,2500)", "");
                    Thread.sleep(200);
                }
                title = driver.findElement(By.className("tb-main-title")).getText();
                price = driver.findElement(By.className("tb-rmb-num")).getText();
                number = driver.findElement(By.xpath("//*[@id=\"J_SpanStock\"]")).getText();
                titleImage = driver.findElement(By.xpath("//*[@id=\"J_ImgBooth\"]")).getAttribute("src");
                System.out.println("标题:"+title);
                System.out.println("价格：" + price);
                System.out.println("库存："+number);
                System.out.println("标题图片:" + titleImage);
                try {
                    boolean displayed = driver.findElement(By.id("tb_attributes")).isDisplayed();
                    if (displayed) {
                        int size = driver.findElements(By.xpath("//*[@id=\"tb_attributes\"]/ul")).size();
                        for (int a = 1; a <= size; a++) {
                            if (driver.findElement(By.xpath("//*[@id=\"tb_attributes\"]/ul[" + a + "]")).isDisplayed()){
                                String text = driver.findElement(By.xpath("//*[@id=\"tb_attributes\"]/ul[" + a + "]")).getText();
                                System.out.println(text);
                            }
                        }
                    }
                }catch (NoSuchElementException e) {
                    if (e.getMessage().contains("Unable to find element with css selector")) {
                        int size = driver.findElements(By.xpath("//*[@id=\"attributes\"]/ul/li")).size();
                        for (int i=1;i<=size;i++) {
                            String s = driver.findElement(By.xpath("//*[@id=\"attributes\"]/ul/li["+i+"]")).getText();
                            System.out.println(s);
                        }
                    }
                }
                List<WebElement> imgList = driver.findElements(By.tagName("img"));
                for (WebElement webElementImage: imgList) {
                    String src = webElementImage.getAttribute("src");
                    if (src.contains("https://img.alicdn.com/imgextra/") && !src.contains("png")&& !src.contains("jpg?")){
                        System.out.println(src);
                    }
                }
            } catch (NoSuchElementException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                // 关闭浏览器
                driver.quit();
                // 关闭 ChromeDriver 接口
                internetExplorerDriverService.stop();
            }
        } else if (url.contains("tmall") || url.contains("yao")) {
            driver.get("https://login.taobao.com/member/login.jhtml?redirectURL=https%3A%2F%2Fai.taobao.com%2F%3Fpid%3Dmm_121122127_40670994_68992200449");
            driver.findElement(By.xpath("//*[@id=\"J_SubmitQuick\"]")).click();
            driver.get(url);
            try {
                driver.get(url);
                for (int i = 0; i < 10; i++) {
                    ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,2500)", "");
                    Thread.sleep(200);
                }
                String title = driver.findElement(By.className("tb-detail-hd")).getText();
                System.out.println("商品名称：" + title);
                try {
                    String price = driver.findElement(By.className("tm-promo-price")).getText();
                    System.out.println("天猫价格:" + price);
                } catch (NoSuchElementException e) {
                    if (e.getMessage().contains("Unable to find element with css selector")) {
                        String price = driver.findElement(By.className("tm-price")).getText();
                        System.out.println("天猫价格：" + price);
                    }
                }
                String titlePicture = driver.findElement(By.xpath("//*[@id=\"J_ImgBooth\"]")).getAttribute("src");
                String number = driver.findElements(By.className("tm-count")).get(0).getText();
                System.out.println("主图片：" + titlePicture);
                System.out.println("销量：" + number);
                int size = driver.findElements(By.xpath("//*[@id=\"J_AttrUL\"]/li")).size();
                for (int a = 1; a <= size; a++) {
                    if (driver.findElement(By.xpath("//*[@id=\"J_AttrUL\"]/li["+ a +"]")).isDisplayed()){
                        String text = driver.findElement(By.xpath("//*[@id=\"J_AttrUL\"]/li["+ a +"]")).getText();
                        System.out.println(text);
                    }
                }
                List<WebElement> imgList = driver.findElements(By.tagName("img"));
                for (WebElement webElementImage: imgList) {
                    String src = webElementImage.getAttribute("src");
                    if (src != null) {
                        if (src.contains("https://img.alicdn.com/imgextra/") && !src.contains("png") && !src.contains("jpg?")){
                            System.out.println(src);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 关闭浏览器
                driver.quit();
                // 关闭 ChromeDriver 接口
                internetExplorerDriverService.stop();
            }
        } else if (url.contains("jd")) {
            try {
                driver.get(url);
                for (int i = 0; i < 10; i++) {
                    ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,2500)", "");
                    Thread.sleep(200);
                }
                String title = driver.findElement(By.className("sku-name")).getText();
                String price = driver.findElements(By.className("p-price")).get(0).getText();
                String picture = driver.findElement(By.xpath("//*[@id=\"spec-img\"]")).getAttribute("src");
                String number = driver.findElement(By.xpath("//*[@id=\"comment-count\"]/a")).getText();
                List<WebElement> elements = driver.findElements(By.xpath("//*[@class=\"p-parameter\"]/ul"));
                for (WebElement webElement : elements) {
                    String text = webElement.getText();
                    System.out.println(text);
                }
                List<WebElement> imgList = driver.findElements(By.tagName("img"));
                for (WebElement webElementImage: imgList) {
                    String src = webElementImage.getAttribute("src");
                    if (src != null) {
                        if (src.contains("360buyimg.com") && !src.contains(".png")
                                && !src.contains("sma.jpg") && !src.contains("/n")
                                && !src.contains("/css") && !src.contains(".gif")
                                && !src.contains("/club_community") && !src.contains("https://img11.360buyimg.com/devfe/jfs/t19249/266/555939662/10324/447efd03/5a965eb2Nf83fd68c.jpg")){
                            System.out.println(src);
                        }
                    }
                }
                System.out.println("京东标题：" + title);
                System.out.println("京东价格：" + price);
                System.out.println("商品图片：" + picture);
                System.out.println("累计评价：" + number);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 关闭浏览器
                driver.quit();
                // 关闭 ChromeDriver 接口
                internetExplorerDriverService.stop();
            }
        }
    }

    public static void main(String[] args) throws IOException {
        String url = "https://item.taobao.com/item.htm?spm=a219r.lm5630.14.9.41c2634ccJTZLf&id=543421393992&ns=1&abbucket=17#detail";
        String url1 = "https://chaoshi.detail.tmall.com/item.htm?spm=a3204.7933263.0.0.21dd2258xWMZyW&id=558603558365&rewcatid=50512019";
        String url2 = "https://item.jd.com/34880759341.html";
        DataReptile.dataReptile(url);
    }
}